/***
 * assign方法
 */

const target = {
    x: 0,
    y: 1
};
const source = {
    x: 1,
    z: 2,
    fn: {
        number: 1
    }
};
Object.assign(target, source);
console.log(target)   //目标对象的同名属性会被覆盖 // target  {x : 1, y : 1, z : 2, fn : {number : 1}}

console.log(source)  //{x : 1, z : 2, fn : {number : 1}}
target.fn.number = 2;
console.log(source)  //拷贝为对象引用，浅拷贝 source  {x : 1, z : 2, fn : {number : 2}}


//不会拷贝继承属性
function Person() {
    this.name = 1
};
Person.prototype.country = 'china';
let student = new Person();
student.age = 29;
const young = { insterst: 'sport' };
Object.assign(young, student);
console.log(young)   // young {instest : 'sport' , age : 29, name: 1}        
 
Object.assign([1, 2, 3], [4, 5])      // 把数组当作对象来处理
// [4, 5, 3]


/**
 * create方法
 */
var parent = {
    x: 1,
    y: 1
}

var child = Object.create(parent, {
    z: {                           // z会成为创建对象的属性
        writable: true,
        configurable: true,
        value: "newAdd"
    }
});

console.log(child)

/***
 * defineProperties()方法
 */

var obj = {};
Object.defineProperties(obj, {
  'property1': {
    value: true,
    writable: true
  },
  'property2': {
    value: 'Hello',
    writable: false
  }
  // etc. etc.
});
console.log(obj)   // {property1: true, property2: "Hello"}


/**
 * defineProperty()方法
 */
let obj1={}
 Object.defineProperty(obj1, 'is', {
    value: function(x, y) {
      if (x === y) {
        // 针对+0 不等于 -0的情况
        return x !== 0 || 1 / x === 1 / y;
      }
      // 针对NaN的情况
      return x !== x && y !== y;
    },
    configurable: true,
    enumerable: false,
    writable: true 
  }); 

  console.log(obj1.is(1,2))

 /**
 * defineProperty()方法 set get
 */ 
  let obj2={}
  Object.defineProperty(obj2, 'is', {
    get(){
        return "val"
    },
    set(val){
        return val
    },
    configurable: true,
    enumerable: false,
    // writable: true 
  }); 
  console.log(obj2)

  // 注意不能同时设置(writable，value) 和 get，set方法，否则浏览器会报错 ： Invalid property descriptor. Cannot both specify accessors and a value or writable attribute
